//
//  Quake+edit.swift
//  coredataTest
//
//  Created by xuNick on 16/11/10.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

extension Quake{

    func updateFromDictionary(_ quakeDictionary: [String: AnyObject]) {
        // Only update the quake if all the relevant properties can be accessed.
        guard let properties = quakeDictionary["properties"] as? [String: AnyObject],
            let newCode = properties["code"] as? String,
            let newMagnitude = properties["mag"] as? Float,
            let newPlaceName = properties["place"] as? String,
            let newDetailURL = properties["detail"] as? String,
            let newTime = properties["time"] as? Double,
            let geometry = quakeDictionary["geometry"] as? [String: AnyObject],
            let coordinates = geometry["coordinates"] as? [Float] else {
                
                //            let localizedDescription = NSLocalizedString("Could not interpret data from the earthquakes server.", comment: "")
                return
                
        }
        
        code = newCode
        magnitude = newMagnitude
        placeName = newPlaceName
        detailURL = newDetailURL
        time = Date(timeIntervalSince1970: newTime / 1000.0) as NSDate?
        
        /*
         The longitude, latitude, and depth values are stored in an array in JSON.
         Access these values by index directly.
         */
        longitude = coordinates[0]
        latitude = coordinates[1]
        depth = coordinates[2]
    }

}
